/************************************************************************************/
/*  Program name CAPGLIMP_ALLOC_FORMULA_LINE.sql                                    */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 25 JAN, 2006   NIP Karen         First created                                   */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_ALLOC_FORMULA_LINE';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_ALLOC_FORMULA_LINES;

BEGIN
	FOR C1 IN Cur_F LOOP
		BEGIN
			INSERT INTO GL_ALLOC_FORMULA_LINES
			VALUES
			(
			CAPBRIMP_MAPPING.GetGLAllocFormulaIDMapping(C1.allocation_formula_id),
			C1.line_number,
			C1.line_type,
			C1.operator,
			C1.last_update_date,
			CAPBRIMP_COMMON.GetUser(C1.last_updated_name),
			C1.creation_date,
			CAPBRIMP_COMMON.GetUser(C1.created_name),
			C1.last_update_login,
			C1.amount,
			C1.relative_period,
			C1.period_name,
			C1.currency_code,
			C1.transaction_currency,
			C1.actual_flag,
--			CAPBRIMP_COMMON.GetBudgetVerID(C1.BUDGET_NAME),	-- delete "C1.budget_version_id if this line is used
			C1.budget_version_id,
--			CAPBRIMP_COMMON.GetEncumTypeID(C1.ENCUM_TYPE),	-- delete "C1.encumbrance_type_id if this line is used
			C1.encumbrance_type_id,
			C1.amount_type,
			C1.segment_types_key,
			C1.segment_break_key,
			C1.segment1,
			C1.segment2,
			C1.segment3,
			C1.segment4,
			C1.segment5,
			C1.segment6,
			C1.segment7,
			C1.segment8,
			C1.segment9,
			C1.segment10,
			C1.segment11,
			C1.segment12,
			C1.segment13,
			C1.segment14,
			C1.segment15,
			C1.segment16,
			C1.segment17,
			C1.segment18,
			C1.segment19,
			C1.segment20,
			C1.segment21,
			C1.segment22,
			C1.segment23,
			C1.segment24,
			C1.segment25,
			C1.segment26,
			C1.segment27,
			C1.segment28,
			C1.segment29,
			C1.segment30
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;
	
	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/